<?php
class koudai_order_cartControl extends skymvc{
	public $oc_where;
	public $userid;
	public function __construct(){
		parent::__construct();
		
	}
	
	public function onInit(){
		$this->oc_where=M("login")->userid?" (userid=".M("login")->userid." or oc_ssid='".OC_SSID."')  ":"oc_ssid='".OC_SSID."' ";
		$this->oc_where.=" AND siteid=".SITEID;
		$this->userid=M("login")->userid;
	}
	
	public function onDefault(){
		$shopid=get('shopid','i');
		$koudai=M('koudai')->selectRow("id=".$shopid);
		$where=$this->oc_where." AND shopid=".$shopid;
		$cart=M('koudai_order_cart')->select(array(
			"where"=>$where
		));
		 
		$total_money=0; 
		if($cart){
			foreach($cart as $k=>$v){
				$productids[]=$v['productid'];
				$ids[]=$v['object_id'];
			}
			$products=M('product')->getListByIds($productids);
			$koudai_products=M('koudai_product')->id_List(array("where"=>"id in("._implode($ids).")"));
			 
			foreach($cart as $k=>$v){
				$v['product']=$product=$products[$v['productid']];
				 
				$v['price']=$product['lower_price']>0?$product['lower_price']:$koudai_products[$v['object_id']]['price'];
				$v['total_money']=$v['price']*$v['amount'];
				$total_money+=$v['total_money'];
				$cart[$k]=$v;
			}
			$product_num=count($cart);
		}
		$this->smarty->assign(array(
			"koudai"=>$koudai,
			"cart"=>$cart,
			"total_money"=>$total_money,
			"product_num"=>$product_num
		));
		$this->smarty->display("koudai_order_cart/index.html");
	}
	
	public function onGetCart(){
		 
		$this->smarty->display("koudai_order_cart/cart.html");
	}
	
	public function onAdd(){
		$data['object_id']=$object_id=get_post('object_id','i');
		$data['shopid']=$shopid=get_post('shopid','i');
		$data['productid']=$productid=get_post('productid','i');
		$data['siteid']=SITEID;
		if(!$object_id or !$shopid or !$productid) exit(json_encode(array("error"=>1,"status"=>"failed","message"=>"商品参数错误")));
		$ymproduct=M("koudai_product")->selectRow(array("where"=>" status=2 AND id=".$object_id));
		if(empty($ymproduct)) exit(json_encode(array("error"=>1,"status"=>"failed","message"=>"商品已下线")));
		//判断商品数量
		$amount=$data['amount']=get_post('amount','i');
		$row=M("koudai_order_cart")->selectRow(array("where"=>$this->oc_where."  AND object_id=".$object_id." ")); 
		if($ymproduct['total_num']<$data['amount'] && $row['amount']<$data['amount']){
			exit(json_encode(array("error"=>1,"status"=>"failed","message"=>"商品库存不足")));
		}
		 if($amount>MAX_BUY_NUM){
			$this->goAll("购买数量不能大于".MAX_BUY_NUM);
		}
		$data['dateline']=time();
		$data['userid']=M("login")->userid;		
		$data['shopid']=$ymproduct['shopid'];
		$data['oc_ssid']=OC_SSID;
		 
		$row=M("koudai_order_cart")->selectRow(array("where"=>$this->oc_where."  AND object_id=".$object_id." "));  
		if($row){
			if(get_post('amount','i')==0){
				M("koudai_order_cart")->delete("id=".$row['id']);
			}else{
				M("koudai_order_cart")->update(array("amount"=>$data['amount']),"id=".$row['id']);
			}
			$isupdate=1;
		}else{
			$isupdate=0;
			$cart_id=M("koudai_order_cart")->insert($data);
		}
		
		$this->loadControl("koudai_order_cartapi");
		$data=$this->koudai_order_cartapiControl->cart();
		
		exit(sky_json_encode(array("error"=>0,"message"=>"加入成功","status"=>"success","data"=>$data)));
	}
	 
	
	public function onDelete(){
		$id=get_post('id','i');
		$data=M("koudai_order_cart")->selectRow(array("where"=>$this->oc_where." AND id=".$id." "));
		if(empty($data)){
			exit(json_encode(array("error"=>1,"message"=>"购物车不存在该商品")));
		}
		M("koudai_order_cart")->delete("id=$id  ");
		$this->loadControl("koudai_order_cartapi");
		$cart=$this->koudai_order_cartapiControl->cart();
		exit(json_encode(array("error"=>0,"message"=>"删除成功","total_money"=>$cart['total_money'])));
	}
	
	public function onClear(){
		$shopid=get_post('shopid','i');
		$where=$this->oc_where;
		if($shopid){
			$where .=" AND shopid=".$shopid;
		}
		M("koudai_order_cart")->delete($where);
		exit(json_encode(array("error"=>0,"message"=>"删除成功")));
	}
	
	public function onNum_plus(){
		$id=get_post('id','i');
		$num=max(1,get_post('num'));
		$data=M("koudai_order_cart")->selectRow(array("where"=>$this->oc_where." AND id=".$id." "));
		if(empty($data)){
			exit(json_encode(array("error"=>1,"message"=>"购物车不存在该商品")));
		}
		$ymproduct=M("koudai_product")->selectRow(array("where"=>" status=2 AND id=".$data['object_id']));
		if(empty($ymproduct)) exit(json_encode(array("error"=>1,"message"=>"商品已下线")));
		//判断商品数量
		if($ymproduct['total_num']< ($data['amount']+$num) ){
			exit(json_encode(array("error"=>1,"message"=>"商品库存不足")));
		}
		
		$res=M("koudai_order_cart")->changenum("amount",$num,"   id=".$id." " );
		$this->loadControl("koudai_order_cartapi");
		$cart=$this->koudai_order_cartapiControl->cart();
		if($res){
			exit(json_encode(array("error"=>0,"message"=>"增加数量成功","total_money"=>$cart['total_money'])));
		}else{
			exit(json_encode(array("error"=>1,"message"=>"增加数量失败")));
		}
	}
	
	public function onnum_minus(){
		$data['object_id']=$object_id=get_post('object_id','i');
		$data['shopid']=$shopid=get_post('shopid','i');
		$data['productid']=$productid=get_post('productid','i');
		$num=max(1,get_post('num'));
		$data=M("koudai_order_cart")->selectRow(array("where"=>$this->oc_where." AND object_id=".$object_id." "));
		if(empty($data)){
			exit(json_encode(array("error"=>1,"message"=>"购物车不存在该商品")));
		}
		if($data['amount']-$num<=0){
			exit(json_encode(array("error"=>1,"message"=>"商品数量至少1件")));
		}
		$res=M("koudai_order_cart")->changenum("amount","-".$num, "   id=".$data['id']." " );
		$this->loadControl("koudai_order_cartapi");
		$cart=$this->koudai_order_cartapiControl->cart($shopid);
		if($res){
			exit(json_encode(array("error"=>0,"message"=>"削减数量成功","data"=>$cart)));
		}else{
			exit(json_encode(array("error"=>1,"message"=>"削减数量失败")));
		}
	}
	
}
?>